package demo;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: zjq
 * @Date: create in 2024/5/8 15:35
 */
public class DataModelUtil {


    @Test
    public void test2() {
        String originalString = "\t`id` BIGINT(20) NOT NULL COMMENT '主键id',\n" +
                "\t`name` VARCHAR(200) NOT NULL  COMMENT '类型名称' COLLATE 'utf8_general_ci',\n" +
                "\t`org_id` VARCHAR(64) NOT NULL  COMMENT '所属组织' COLLATE 'utf8_general_ci',\n" +
                "\t`deleted` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '删除标记(0否1是)',\n" +
                "\t`gmt_create` TIMESTAMP NOT NULL  COMMENT '创建时间',\n" +
                "\t`create_user` VARCHAR(64) NOT NULL  COMMENT '创建人' COLLATE 'utf8_general_ci',\n" +
                "\t`gmt_modify` TIMESTAMP NOT NULL  COMMENT '修改时间',\n" +
                "\t`modify_user` VARCHAR(64) NOT NULL  COMMENT '修改人' COLLATE 'utf8_general_ci',\n" +
                "\tPRIMARY KEY (`id`) USING BTREE";
        String modifiedString = modifyCommentContents(originalString);
        System.out.println("===" + modifiedString);

    }

    // 修改注释内容为不包含任何空格的内容。
    public static String modifyCommentContents(String input) {
        // 正则表达式直接匹配 COMMENT 后面单引号内的内容
        Pattern pattern = Pattern.compile("(?i)COMMENT\\s*'([^']*)'");
        Matcher matcher = pattern.matcher(input);

        StringBuffer modifiedInput = new StringBuffer();
        while (matcher.find()) {
            // 提取内容并移除空格
            String contentWithSpaces = matcher.group(1);
            String contentWithoutSpaces = contentWithSpaces.replaceAll("\\s", "");
            // 替换原始字符串中的COMMENT后的内容
            matcher.appendReplacement(modifiedInput, "COMMENT '" + contentWithoutSpaces + "'");
        }
        matcher.appendTail(modifiedInput);
        return modifiedInput.toString();
    }

    @Test
    public void test() {
        String startStr =
                "\t`id` BIGINT(19) NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
                        "\t`xxid` INT(10) NULL DEFAULT NULL COMMENT '学校id',\n" +
                        "\t`year` VARCHAR(50) NULL DEFAULT NULL COMMENT '学年' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`grade` VARCHAR(50) NULL DEFAULT NULL COMMENT '年级编号' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`classnum` VARCHAR(50) NULL DEFAULT NULL COMMENT '班级编号' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`classname` VARCHAR(50) NULL DEFAULT NULL COMMENT '班级名称' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`grbsm` VARCHAR(50) NULL DEFAULT NULL COMMENT '学籍号' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`mz` VARCHAR(50) NULL DEFAULT NULL COMMENT '民族代码' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`xm` VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`xb` VARCHAR(50) NULL DEFAULT NULL COMMENT '性别' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`csrq` VARCHAR(50) NULL DEFAULT NULL COMMENT '出生日期' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`xsly` VARCHAR(50) NULL DEFAULT NULL COMMENT '学生来源' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`sg` VARCHAR(50) NULL DEFAULT NULL COMMENT '身高' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`zt` VARCHAR(50) NULL DEFAULT NULL COMMENT '体重' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`fhl` VARCHAR(50) NULL DEFAULT NULL COMMENT '肺活量' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`wsmp` VARCHAR(50) NULL DEFAULT NULL COMMENT '五十米跑' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`ldty` VARCHAR(50) NULL DEFAULT NULL COMMENT '立定跳远' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`zwtqq` VARCHAR(50) NULL DEFAULT NULL COMMENT '坐位体前屈' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`bbmp` VARCHAR(50) NULL DEFAULT NULL COMMENT '八百米跑' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`yqmp` VARCHAR(50) NULL DEFAULT NULL COMMENT '一千米跑' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`yfzywqz` VARCHAR(50) NULL DEFAULT NULL COMMENT '一分钟仰卧起坐' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`ytxs` VARCHAR(50) NULL DEFAULT NULL COMMENT '引体向上' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`yfzts` VARCHAR(50) NULL DEFAULT NULL COMMENT '一分钟跳绳' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\t`wfp` VARCHAR(50) NULL DEFAULT NULL COMMENT '往返跑' COLLATE 'utf8mb4_0900_ai_ci',\n" +
                        "\tPRIMARY KEY (`id`) USING BTREE";
        System.out.println(createJson(startStr));
    }


    public static List<Column> createJson(String createSql) {
        String createSql2 = modifyCommentContents(createSql);

        String[] lines = createSql2.split(",\n");
        List<Column> list = new ArrayList<>();
        Arrays.stream(lines).forEach(line -> {
            line = line.trim();
            Column column = new Column();
            List<String> temps = Arrays.asList(line.split(" "));
            if (temps.get(0).contains("`")) {
                list.add(column);
                column.setColumnName(temps.get(0).replace("`", ""));
                int i = temps.indexOf("COMMENT");
                if (i > 0) {
                    String commet = temps.get(i + 1).replace("'", "");
                    column.setComment(commet);
                    String[] foo = commet.split("[ ,;:：，()【】（）]");
                    if (foo.length > 0) {
                        column.setName(foo[0]);
                    }
                    if (line.contains("BIGINT")) {
                        column.setType("大整数");
                        column.setColumnType("BIGINT");
                    } else if (line.contains("INT") && !line.contains("BIGINT")) {
                        column.setType("整数");
                        column.setColumnType("INT");
                    } else if (line.contains("DATETIME")) {
                        column.setType("日期时间");
                        column.setColumnType("DATETIME");
                    } else if (line.contains("VARCHAR(1024)") ) {
                        column.setType("字串-中");
                        column.setColumnType("VARCHAR(1024)");
                    } else if (line.contains("TEXT")) {
                        column.setType("小文本");
                        column.setColumnType("TEXT");
                    } else {
                        column.setType("字串-短");
                        column.setColumnType("VARCHAR(128)");
                    }

                    if (line.contains("NOT NULL")) {
                        column.setNotNull(1);
                    } else {
                        column.setNotNull(0);
                    }

                }
            }
        });

        return list;
    }

    public static class Column {
        private String name;
        private String type;
        private String columnName;
        private String columnType;
        private int notNull;
        private String comment;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public String getColumnType() {
            return columnType;
        }

        public void setColumnType(String columnType) {
            this.columnType = columnType;
        }

        public int getNotNull() {
            return notNull;
        }

        public void setNotNull(int notNull) {
            this.notNull = notNull;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }

        @Override
        public String toString() {
            return "{" +
                    '"' + "name" + "\": " + '"' + name + '"' + "," +
                    '"' + "type" + "\": " + '"' + type + '"' + "," +
                    '"' + "columnName" + "\": " + '"' + columnName + '"' + "," +
                    '"' + "columnType" + "\": " + '"' + columnType + '"' + "," +
                    '"' + "notNull" + "\": " + notNull + "," +
                    '"' + "comment" + "\": " + '"' + comment + '"' +
                    '}';
        }
    }
}
